package com.allwinter.yibangsafe.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.allwinter.yibangsafe.domain.WxUser;
import com.allwinter.yibangsafe.mapper.UserMapper;
import com.allwinter.yibangsafe.domain.User;
import com.allwinter.yibangsafe.mapper.WxUserMapper;
import com.allwinter.yibangsafe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private WxUserMapper wxUserMapper;

    /**
     * @param :
     * @return List<User>
     * @author 杨雪松
     * @description 获取所有User
     * @date
     */
    public List<User> getUsers() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    public User getById(Long userid) {
        User user = userMapper.selectById(userid);
        return user;
    }

    public boolean saveUser(User user) {
        boolean res = true;
        try {
            userMapper.insert(user);
        } catch (Exception e) {
            res = false;
        }
        return res;
    }

    public boolean updateUser(User user) {
        boolean res = true;
        try {
            userMapper.updateById(user);
        } catch (Exception e) {
            res = false;
        }
        return res;
    }

    @Override
    public boolean updateUser(String user) {
        JSONObject userJson0 = JSONObject.parseObject(user);
        String userinfo = userJson0.getString("userinfo");
        JSONObject userJson = JSONObject.parseObject(userinfo);
        User userTemp = new User();
        userTemp.setUserid(userJson.getLong("userid"));
        userTemp.setUsername(userJson.getString("nickname"));
        userTemp.setBirthday(userJson.getString("birthday"));
        userTemp.setPassword(userJson.getString("password"));
        userTemp.setSex(userJson.getString("sex"));
        userTemp.setHobby(userJson.getString("hobby"));
//        userTemp.setAge();
        userTemp.setPhonenum(userJson.getString("phoneNum"));
        userTemp.setProfile(userJson.getString("profile"));

        System.out.println("---------->" + userJson.toString());
        System.out.println("---------->" + userTemp.toString());

        try {
            userMapper.updateById(userTemp);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
